package com.whnk.attendance.controller;

import com.whnk.attendance.service.IAreaInfoService;
import com.whnk.attendance.vo.AreaInfoListVo;
import com.whnk.attendance.vo.AreaListVo;
import com.whnk.attendance.vo.AreaInfoQueryVo;
import com.whnk.attendance.vo.AreaInfoUpdateVo;
import com.whnk.common.core.utils.poi.ExcelUtil;
import com.whnk.common.core.web.controller.BaseController;
import com.whnk.common.core.web.domain.AjaxResult;
import com.whnk.common.core.web.page.TableDataInfo;
import com.whnk.common.log.annotation.Log;
import com.whnk.common.log.enums.BusinessType;
import com.whnk.common.security.annotation.PreAuthorize;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.*;

/**
 * 区域列表Controller
 *
 * @author whnk
 * @date 2021-11-17
 */

@RestController
@RequestMapping("/area")
@Api(tags="区域列表")
public class AreaInfoController extends BaseController{

    @Autowired
    private IAreaInfoService areaInfoService;

    /*
     * 分页查询区域列表
     */
    @PreAuthorize(hasPermi = "attendance:info:list")
    @GetMapping("/list")
    @ApiOperation(value = "分页查询区域列表", notes = "分页查询区域列表")
    public TableDataInfo list(@ApiParam(value = "区域信息", required = false) AreaInfoQueryVo areaInfoQueryVo){
        startPage();
        List<AreaInfoListVo> list = areaInfoService.selectAreaInfoListVoList(areaInfoQueryVo);
        return getDataTable(list);
    }

    /*
     * 新增区域列表
     */
    @PreAuthorize(hasPermi = "attendance:info:add")
    @Log(title = "区域列表", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ApiOperation(value = "新增区域列表",notes = "新增区域列表")
    public AjaxResult add(@ApiParam(value = "区域信息", required = false) @Validated @RequestBody AreaInfoUpdateVo areaInfoUpdateVo){
        if(!areaInfoService.checkAreaId(areaInfoUpdateVo)){
            return AjaxResult.error("新增区域信息" + areaInfoUpdateVo.getAreaId() + "失败，该区域信息已存在");
        }
        return toAjax(areaInfoService.insertAreaInfo(areaInfoUpdateVo));
    }

    /*
     * 根据主键集合删除区域列表
     */
    @PreAuthorize(hasPermi = "attendance:info:remove")
    @Log(title = "区域列表", businessType = BusinessType.DELETE)
    @DeleteMapping("/remove/{areaIds}")
    @ApiOperation(value = "根据主键集合删除区域列表",notes = "根据主键集合删除区域列表")
    public AjaxResult remove(@ApiParam(value = "交付区域编号", required = false) @PathVariable String[] areaIds) {
        return toAjax(areaInfoService.deleteAreaInfoByAreaIds(areaIds));
    }

    /*
     * 修改区域列表
     */
    @PreAuthorize(hasPermi = "attendance:info:edit")
    @Log(title = "区域列表", businessType = BusinessType.UPDATE)
    @PutMapping("/edit")
    @ApiOperation(value = "修改区域列表",notes = "修改区域列表")
    public AjaxResult edit(@ApiParam(value = "区域信息", required = false) @Validated @RequestBody AreaInfoUpdateVo areaInfoUpdateVo) {
        if(!areaInfoService.checkAreaId(areaInfoUpdateVo)){
            return AjaxResult.error("修改区域信息" + areaInfoUpdateVo.getAreaId() + "失败，该区域信息已存在");
        }
        return toAjax(areaInfoService.updateAreaInfo(areaInfoUpdateVo));
    }

    /*
     * 根据区域编号获取区域列表详细信息
     */
    @PreAuthorize(hasPermi = "attendance:info:query")
    @GetMapping("/query/{areaId}")
    @ApiOperation(value = "根据区域编号获取区域列表详细信息",notes = "根据区域编号获取区域列表详细信息")
    public AjaxResult getInfo(@ApiParam(value = "区域编号", required = false) @PathVariable("areaId") String areaId){
        return AjaxResult.success(areaInfoService.selectAreaInfoListVoByAreaId(areaId));
    }

    /*
     * 导出区域列表
     */
    @PreAuthorize(hasPermi = "attendance:info:export")
    @Log(title = "区域列表", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ApiOperation(value = "导出区域列表",notes = "导出区域列表")
    public void export(HttpServletResponse response, @ApiParam(value = "区域信息", required = false) AreaInfoQueryVo areaInfoQueryVo) throws IOException
    {
        List<AreaInfoListVo> list = areaInfoService.selectAreaInfoListVoList(areaInfoQueryVo);
        ExcelUtil<AreaInfoListVo> util = new ExcelUtil<AreaInfoListVo>(AreaInfoListVo.class);
        util.exportExcel(response, list, "区域信息数据");
    }

    /*
     * 导入区域列表
     */
    @PreAuthorize(hasPermi = "attendance:info:import")
    @Log(title = "区域列表", businessType = BusinessType.IMPORT)
    @PostMapping("/import")
    @ApiOperation(value = "导入区域列表",notes = "导入区域列表")
    public AjaxResult importData(@RequestPart("file") MultipartFile file, boolean updateSupport) throws Exception
    {
        ExcelUtil<AreaInfoListVo> util = new ExcelUtil<>(AreaInfoListVo.class);
        List<AreaInfoListVo> areaInfoList = util.importExcel(file.getInputStream());
        String message = areaInfoService.importData(areaInfoList, updateSupport);
        return AjaxResult.success(message);
    }

    /*
     * 交付区域编号id，交付区域名称name接口
     */
    @GetMapping("/areaList")
    @ApiOperation(value = "交付区域编号id，交付区域名称name接口",notes = "交付区域编号id，交付区域名称name接口")
    public TableDataInfo areaList(){
        List<AreaListVo> list = areaInfoService.areaList();
        return getDataTable(list);
    }
}

